= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 A u t h o r :   Y o u n g w o o k   K i m   ( K o r e a n ) 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 C o n t a c t :   r u m i a 0 6 0 1 @ g m a i l . c o m 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 w i t h   A d v a n c e d   P R O C E S S   -   F u n c t i o n a l i z a t i o n 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 F i r s t ,   L e t  s   p r i n t   v i s u a l i z e d   g e o m e t r y ,   n o t   t e x t .   H o w   a b o u t   H P   b a r ?   I f   m a x   H P   o f   g a m e   i s   f i x e d   a n d   c u r r e n t   H P   o f   g a m e   c a n   v a r y   f r o m   0   t o   m a x   H P ,   w h a t   w i l l   b e   s i m p l e s t   w a y   t o   p r i n t   b o t h   t w o   d a t a ? 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t P r o c e s s 1 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t P r o c e s s 1 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t P r o c e s s 2 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t P r o c e s s 2 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t P r o c e s s 3 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t P r o c e s s 3 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 Y e a h ,   j u s t   r e - r e n d e r i n g   t e x t   w h e n   c e r t a i n   v a r i a b l e   i s   c h a n g e d .   H o w   t o   c h a n g e   v a r i a b l e ?   T h a t   l o g i c   i s   i n s i d e   o f   E v e n t   s t a t e m e n t .   ( P r e s s i n g   u p   o r   d o w n   t o   a d j u s t   H P . )   S a m e   m e t h o d   a s   b e f o r e .   B u t   t h e y   a r e   s t i l l   t e x t ,   w h i c h   m e a n s   t h e y   a r e   n o t   v i s u a l i z e d   e n o u g h .   H o w   t o   v i s u a l i z e   t h e s e   t w o   d a t a   m o r e   d e t a i l   ( m a x   H P ,   c u r r e n t   H P ) ?   W e   c a n   u s e   i d e a   o f   m a g a z i n e   ( g u n  s   m a g a z i n e ) .   H P   i s   i n t e g e r   v a l u e ,   w h i c h   i s   d i s c r e t e .   S o ,   i t   c a n   b e   p r i n t e d   a s   b e l o w : 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t P r o c e s s 4 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t P r o c e s s 4 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t P r o c e s s 5 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t P r o c e s s 5 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 . .   i m a g e : :   A d v a n c e d O u t p u t P r o c e s s 6 . g i f 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   3 2 0 ,   2 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " A d v a n c e d O u t p u t P r o c e s s 6 . g i f " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 
 
 M u c h   b e t t e r ,   i s n  t   i t ?   D r a w i n g   l o g i c   i s   i n s i d e   o f   # 6 .   L o g i c   i s   s i m p l e ,   f i r s t ,   d r a w   a   * * b i g   r e c t a n g u l a r * *   w h i c h   c o l o r   i s   b l a c k .   T h e n ,   c h e c k   w h e t h e r   d r a w   a   * * s m a l l e r   r e c t a n g u l a r * *   w h i c h   c o l o r   i s   r e d   o r   n o t   d e p e n d i n g   o n   c u r r e n t   H P .   F i n a l l y ,   d r a w   * * m a r g i n * *   o f   c u r r e n t   s m a l l   r e c t a n g u l a r .   M a r g i n   o f   c o l o r   i s   w h i t e .   W e   c a n   f i n d   t h e r e   a r e   4   p a r a m e t e r s   f o r   l o c a t i o n   d a t a   i n   ` ` p y g a m e . d r a w . r e c t ` `   ( I   m e a n ,   t h i r d   d a t a .   F i r s t   d a t a   i s   c a n v a s   d a t a ,   s e c o n d   d a t a   i s   c o l o r   d a t a   a n d   f o u r t h   d a t a   i s   w i d t h . )   T h e   e a s i e s t   w a y   t o   u n d e r s t a n d   4   p a r a m e t e r s   i s   c h a n g e   t h e m .   C h a n g e   o n e   v a l u e   i n t o   1 0   o r   3 0   w h i l e   o t h e r s   a r e   2 0 !   T h e n   i t   c a n   b e   u n d e r s t o o d .   N e e d l e s s   t o   e x p l a i n . 
 
 
 
 F u r t h e r m o r e ,   n o w   i t  s   t i m e   t o   f u n c t i o n a l i z e   s p e c i f i c a l l y .   I   p u s h   A l w a y s   s t a t e m e n t   a n d   E v e n t   s t a t e m e n t   i n t o   m a i n   f u n c t i o n .   ( # 7   i s   n e e d e d   t o   f i n d   m a i n   f u n c t i o n   a n d   e x e c u t e   i t . )   A n d   m a d e   n e w   f u n c t i o n   f o r   d r a w H P .   F u n c t i o n a l i z a t i o n   i d e a   f o r   g a m e   i s   n o t   f a r   a w a y   f r o m   t h a t   o f   n o r m a l   G U I   p r o g r a m .   F o r   e x a m p l e ,   i t   i s   b e t t e r   t o   m a k e   * * s i n g l e   p r i n t   f u n c t i o n   f o r   e v e r y   s i n g l e   v a l u e * *   w h i c h   h a s   t o   b e   d i s p l a y e d .   O f   c o u r s e ,   s e t t i n g   l o c a t i o n   f o r   e a c h   v a l u e   h a s   t o   b e   d o n e   w h i l e   y o u   a r e   d e s i g n i n g   t h e   s c r e e n . 
 
 
 
 < R e f e r e n c e   C o d e >   : : 
 
 
 
         i m p o r t   p y g a m e ,   s y s 
 
         f r o m   p y g a m e . l o c a l s   i m p o r t * 
 
         
 
         m a x H P   =   1 0   
 
         w h i t e   =   ( 2 5 5 , 2 5 5 , 2 5 5 ) 
 
         g r a y   =   ( 1 2 7 , 1 2 7 , 1 2 7 ) 
 
         b l a c k   =   ( 0 , 0 , 0 ) 
 
         r e d   =   ( 2 5 5 , 0 , 0 ) 
 
         g r e e n   =   ( 0 , 2 5 5 , 0 ) 
 
         b l u e   =   ( 0 , 0 , 2 5 5 ) 
 
         p y g a m e . i n i t ( ) 
 
         p y g a m e . d i s p l a y . s e t _ c a p t i o n ( " H P   b a r   P r o j e c t " ) 
 
         w i d t h   =   6 4 0   # 1 
 
         h e i g h t   =   4 8 0   # 2 
 
         m y S c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( ( w i d t h ,   h e i g h t ) ) 
 
         m y T e x t F o n t   =   p y g a m e . f o n t . F o n t ( " H o o n W h i t e c a t R . t t f " ,   3 2 ) 
 
         m y T e x t   =   m y T e x t F o n t . r e n d e r ( ( s t r ( m a x H P )   +   " / "   +   s t r ( m a x H P ) ) ,   T r u e ,   r e d ,   g r a y ) 
 
         m y T e x t A r e a   =   m y T e x t . g e t _ r e c t ( ) 
 
         m y T e x t A r e a . c e n t e r   =   ( w i d t h / 2 ,   h e i g h t / 2 )   # 3 
 
         f p s C l o c k   =   p y g a m e . t i m e . C l o c k ( ) 
 
         
 
         d e f   m a i n ( ) :   # 4 
 
                 H P   =   5 
 
                 
 
                 w h i l e   T r u e : 
 
                         m y T e x t   =   m y T e x t F o n t . r e n d e r ( ( s t r ( H P )   +   " / "   +   s t r ( m a x H P ) ) ,   T r u e ,   r e d ,   g r a y ) 
 
                 
 
                         m y S c r e e n . f i l l ( g r a y ) 
 
         
 
                         m y S c r e e n . b l i t ( m y T e x t ,   m y T e x t A r e a ) 
 
                         d r a w H P ( H P )   # 5 
 
         
 
                         f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                                 i f   e v e n t . t y p e   = =   Q U I T : 
 
                                         p y g a m e . q u i t ( ) 
 
                                         s y s . e x i t ( ) 
 
                                 e l i f   e v e n t . t y p e   = =   K E Y D O W N : 
 
                                         i f   e v e n t . k e y   = =   K _ U P : 
 
                                                 i f   H P   ! =   1 0 : 
 
                                                         H P   =   H P   +   1 
 
                                         e l i f   e v e n t . k e y   = =   K _ D O W N : 
 
                                                 i f   H P   ! =   0 : 
 
                                                         H P   =   H P   -   1 
 
                 
 
                         p y g a m e . d i s p l a y . u p d a t e ( ) 
 
                         f p s C l o c k . t i c k ( 6 0 ) 
 
         
 
         d e f   d r a w H P ( H P ) :   # 6 
 
                 r   =   i n t ( ( h e i g h t   -   4 0 )   /   m a x H P ) 
 
         
 
                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   b l a c k ,   ( 2 0 ,   2 0 ,   2 0 ,   2 0   +   ( ( m a x H P   -   0 . 5 )   *   r ) ) ) 
 
         
 
                 f o r   i   i n   r a n g e ( m a x H P ) : 
 
                         i f   H P   > =   ( m a x H P   -   i ) : 
 
                                 p y g a m e . d r a w . r e c t ( m y S c r e e n ,   r e d ,   ( 2 0 ,   2 0   +   ( i   *   r ) ,   2 0 ,   r ) ) 
 
                         p y g a m e . d r a w . r e c t ( m y S c r e e n ,   w h i t e ,   ( 2 0 ,   2 0   +   ( i   *   r ) ,   2 0 ,   r ) ,   1 ) 
 
         
 
                 r e t u r n 
 
         
 
         i f   _ _ n a m e _ _   = =   ' _ _ m a i n _ _ ' :   # 7 
 
                 m a i n ( ) 
 
         